﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Core;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Mvc.Html;
using Microsoft.Ajax.Utilities;
using Newtonsoft.Json;

namespace WebServiceTMS.Helpers
{
    public class TMSMain
    {
        public TMSGoal Goal { get; set; }
        public TMSTaskHelper[] Task { get; set; }
    }

    public class TMSTaskHelper
    {
        public string TaskName { get; set; }
        public string TaskId { get; set; }
    }

    public class TMSMainHelper
    {
        public Goals Goal { get; set; }
        public Tasks[] Task { get; set; }
    }

    public class MainPageHelper
    {
        public static HttpResponseMessage GetMainPageInfoHelper(int userId)
        {
            try
            {
                using (var context=new TMSEntities())
                {
                    var user = context.Users.SingleOrDefault(u => u.Id == userId);
                    if (user != null)
                    {
                        var allGoals = (from g in context.Goals
                                        join team in context.Teams on g.Id equals team.Goal
                                        where team.TeamMember == userId
                                        select g).ToArray();
                        var queryResult = new TMSMainHelper[allGoals.Count()];
                        for (var i = 0; i < allGoals.Count(); i++)
                        {
                            var id = allGoals[i].Id;
                            queryResult[i] = new TMSMainHelper
                            {
                                Goal = allGoals[i],
                                Task = context.Tasks.Where(t => t.Goal == id).ToArray()
                            };
                        }
                        if (queryResult.Any())
                        {
                            var mainResult = new TMSMain[queryResult.Count()];
                            for (var i = 0; i < queryResult.Count(); i++)
                            {
                                mainResult[i] = new TMSMain
                                {
                                    Goal = new TMSGoal
                                    {
                                        Name = queryResult[i].Goal.Name,
                                        Id = TMSCryptography.Encrypt(queryResult[i].Goal.Id.ToString()),
                                    },
                                    Task = MainPageTaskConverter(queryResult[i].Task)
                                };
                            }
                            var jsonResponse = JsonConvert.SerializeObject(mainResult);
                            return HttpResponseHelper.CreateHttpResponse("success", jsonResponse);
                        }
                        return HttpResponseHelper.CreateHttpResponse("success", "[]");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }

        public static TMSTaskHelper[] MainPageTaskConverter(Tasks[] tasks)
        {
            var result = new TMSTaskHelper[tasks.Count()];
            for (var i = 0; i < tasks.Count(); i++)
            {
                result[i] = new TMSTaskHelper
                {
                    TaskName = tasks[i].Name,
                    TaskId = TMSCryptography.Encrypt(tasks[i].Id.ToString())
                };
            }
            return result;
        }

        public static HttpResponseMessage GetMainPageInfoAdminHelper(int userId)
        {
            try
            {
                using (var context = new TMSEntities())
                {
                    var user = context.Users.SingleOrDefault(u => u.Id == userId);
                    if (user != null)
                    {
                        if (user.Status == "Admin")
                        {
                            var allGoals = (from g in context.Goals
                                select g).ToArray();
                            var queryResult = new TMSMainHelper[allGoals.Count()];
                            for (var i = 0; i < allGoals.Count(); i++)
                            {
                                var id = allGoals[i].Id;
                                queryResult[i] = new TMSMainHelper
                                {
                                    Goal = allGoals[i],
                                    Task = context.Tasks.Where(t => t.Goal == id).ToArray()
                                };
                            }
                            if (queryResult.Any())
                            {
                                var mainResult = new TMSMain[queryResult.Count()];
                                for (var i = 0; i < queryResult.Count(); i++)
                                {
                                    mainResult[i] = new TMSMain
                                    {
                                        Goal = new TMSGoal
                                        {
                                            Name = queryResult[i].Goal.Name,
                                            Id = TMSCryptography.Encrypt(queryResult[i].Goal.Id.ToString()),
                                        },
                                        Task = MainPageTaskConverter(queryResult[i].Task)
                                    };
                                }
                                var jsonResponse = JsonConvert.SerializeObject(mainResult);
                                return HttpResponseHelper.CreateHttpResponse("success", jsonResponse);
                            }
                            return HttpResponseHelper.CreateHttpResponse("success", "[]");
                        }
                        return HttpResponseHelper.CreateHttpResponse("error", "You have no rights to view this list");
                    }
                    return HttpResponseHelper.CreateHttpResponse("error", "There is no such user!");
                }
            }
            catch (Exception ex)
            {
                return HttpResponseHelper.CreateHttpResponse("server_error", ex.Message);
            }
        }
    }
}